import updateManager from "./common/updateManager";
import { login } from "./utils/auth";

function getSiteInfo(): IAppOption["siteInfo"] {
  const versionInfo = wx.getAccountInfoSync();
  const { version, envVersion } = versionInfo.miniProgram;
  const envConfig: { [key: string]: IAppOption["siteInfo"] } = {
    // 正式
    release: {
      uniacid: 2,
      version: version,
      baseUrl: "https://dem.wifixc.com",
    },
    // 体验
    trial: {
      uniacid: 2,
      version: version,
      baseUrl: "https://dem.wifixc.com",
    },
    // 本地开发
    default: {
      uniacid: 1,
      version: "1.0.0",
      baseUrl: "http://dem.wifixc.test",
    },
  };

  return envConfig[envVersion] || envConfig.default;
}

App<IAppOption>({
  onLaunch() {
    // 异步获取系统信息，并存到全局（避免重复调用）
    this.globalData.systemInfo = wx.getSystemInfoSync();

    // 用户登陆
    wx.showLoading({ title: '加载中' });
    login()
      .then((userInfo: any) => {
        if (userInfo) {
          this.globalData.userInfo = userInfo;

          // 用户信息回调
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(userInfo);
          }
        }
      })
      .finally(() => {
        wx.hideLoading();
      });
  },

  onShow() {
    updateManager();
  },

  globalData: {
    systemInfo: undefined,
    userInfo: undefined,
  },

  siteInfo: getSiteInfo(),
});
